package com.huangyi.rabbitmq_amqp.topic;

import com.huangyi.rabbitmq_amqp.config.Constants;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);
        factory.setUsername(Constants.USER);
        factory.setPassword(Constants.PASSWORD);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(Constants.TOPIC_QUEUE1, true, false, false, null);
        channel.queueDeclare(Constants.TOPIC_QUEUE2, true, false, false, null);
        channel.exchangeDeclare(Constants.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC,true);

        channel.queueBind(Constants.TOPIC_QUEUE1,Constants.TOPIC_EXCHANGE,"*.x.*");
        channel.queueBind(Constants.TOPIC_QUEUE2,Constants.TOPIC_EXCHANGE,"*.*.y");
        channel.queueBind(Constants.TOPIC_QUEUE2,Constants.TOPIC_EXCHANGE,"xy.#");

        String msg = "hello this is *.x.*";
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"a.x.b",null,msg.getBytes());
        String msg1 = "hello this is *.*.y";
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"a.b.y",null,msg1.getBytes());
        String msg2 = "hello this is xy.#";
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"ab.x.y",null,msg2.getBytes());
        channel.close();
        connection.close();

    }
}
